package com.yipao.wx.web.operate;

import com.yipao.core.util.ResponseUtil;
import com.yipao.db.domain.devinfo.DevMaterialSlot;
import com.yipao.db.domain.devinfo.vo.DevMaterialSlotList;
import com.yipao.db.service.devinfo.DevFormulaSlotService;
import com.yipao.wx.vo.GoodsVo;
import com.yipao.wx.vo.GoodsVoList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 设备原料配方
 *
 * @author tianhao
 */
@RestController
@RequestMapping("/wx/dev/formula/slot")
@Validated
@Slf4j
public class WxDevFormulaSlotController {


    @Resource
    private DevFormulaSlotService devFormulaSlotService;

    /**
     * 查询指定设备下的设备原料卡槽
     *
     * @param devId 设备id
     * @return
     */
    @GetMapping("list")
    public Object list(@RequestParam @NotNull Integer devId) {
        return ResponseUtil.ok(devFormulaSlotService.queryByDevCode(devId));
    }


    /**
     * 批量更新卡槽的数据
     *
     * @param goodsVoList
     * @return
     */
    @PostMapping("/updates")
    public Object updates(@RequestBody GoodsVoList goodsVoList) {
        int updates = 0;
        List<GoodsVo> list = goodsVoList.getGoodsVoList();
        for (GoodsVo goodsVo : list) {
            updates = devFormulaSlotService.updates(goodsVo.getDevId(), goodsVo.getFormulaIds());
        }
        if (updates > 0) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    /**
     * 修改设备原料槽位信息
     *
     * @param list
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody DevMaterialSlotList list) {
        int updated = 0;
        List<DevMaterialSlot> devFormulaSlots = list.getDevMaterialSlots();
        for (DevMaterialSlot devFormulaSlot : devFormulaSlots) {
            int update = devFormulaSlotService.update(devFormulaSlot);
            if (update > 0) {
                updated += 1;
            }
        }
        if (updated > 0) {
            return ResponseUtil.ok(list);
        }
        return ResponseUtil.fail();
    }

}
